import {useUserStore} from "@/stores/user.js";
const userStore = useUserStore()


//服务器地址
// const baseURL = 'https://tcnet.club:3004';
//调试地址
const baseURL = 'https://localhost:7214';
const fileUrl = 'https://localhost:7214';
const FileById = fileUrl + '/File/GetFileById?id='
const FileUpload = fileUrl + '/File/FileUpload'


function get(url) {
	return new Promise((resolve) => {
		return uni.request({
			url: baseURL + url,
			data: {},
			header: {
				authorization: "Bearer " + userStore.token
			},
			method: "GET",
			success(res) {
				if(res.statusCode == 401){
					uni.navigateTo({
						url: "/pages/login/index"
					})
				}
				resolve(res.data)
			}
		});
	});
};

function post(url, data) {
	return new Promise((resolve) => {
		uni.request({
			url: baseURL + url,
			data: data,
			header: {
				authorization: "Bearer " + userStore.token
			},
			method: "POST",
			success(res) {
				resolve(res.data)
			}
		});
	});
};

function put(url, data) {
	return new Promise((resolve) => {
		uni.request({
			url: baseURL + url,
			data: data,
			header: {
				authorization: "Bearer " + userStore.token
			},
			method: "PUT",
			success(res) {
				resolve(res.data)
			}
		});
	});
};


function del(url,data) {
	return new Promise((resolve) => {
		 uni.request({
			url: baseURL + url,
			header: {
				authorization: "Bearer " + userStore.token
			},
			data: data,
			method: "DELETE",
			success(res) {
				resolve(res.data)
			}
		});
	});
};

const upload = (tempFilePath)=>{
	return new Promise((resolve) => {
		uni.uploadFile({
			url:FileUpload,
			filePath:tempFilePath,
			name: 'file',
			header:{
				'Content-Type': 'multipart/form-data',
				authorization: "Bearer " + userStore.token
			},
			success: (res) => {
				let obj = JSON.parse(res.data)
				resolve(obj)
			}
			
		})
	})
	
}





export default {
	get,
	post,
	put,
	del,
	baseURL,
	FileById,
	upload
}
